<!--
 * @Description: 竣工收尾工作计划列表
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-01-18 14:17:40
 * @LastEditTime: 2022-10-31 15:33:08
 * @FilePath: \cscec-pms-admin\src\views\construction\constructionCompleted\index.vue
-->
<template>
  <base-breadcrumb>
    <!-- 搜索 -->
    <searchData
      :search-data="searchOption"
      @search="search"
    />
    <!-- 搜索 -->
    <a-card>
      <!--      <title-name title="竣工收尾工作计划列表" />-->
      <div class="table-operator">
        <a-button
          v-btnPermission="'pm_044_add_02'"
          type="primary"
          @click="visible = true"
        >
          新增计划
        </a-button>
      </div>
      <div class="pub-table-select-count">
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span style="margin-right: 12px">
          总共：<span class="totalNum">{{ total }}</span>
        </span>
      </div>
      <s-table
        ref="table"
        row-key="orgId"
        :columns="columns"
        :data="loadData"
        show-size-changer
        show-quick-jumper
        :scroll="{ y: 440 }"
        :show-pagination="false"
        :children-column-name="'childTree'"
        :expanded-row-keys="expandedRowKeys"
        @expand="expandedRowsChange"
      >
        <template
          slot="rate"
          slot-scope="text"
        >
          <a-progress
            stroke-linecap="round"
            :percent="text"
            status="active"
          >
            <template #format="text">
              <span>{{ text }}%</span>
            </template>
          </a-progress>
        </template>
        <template
          slot="statusCode"
          slot-scope="text, record"
        >
          <a-badge
            v-if="record.tageCode === 'PD'"
            :status="record.projectVO.status | statusTypeFilter"
            :text="record.projectVO.status | statusFilter"
          />
        </template>
        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-if="record.tageCode == 'PD'"
              v-btnPermission="'pm_044_loo_01'"
              @click="handleDetail(record)"
            >查看</a>
            <a
              v-if="(record.tageCode == 'PD' && (record.projectVO.status=== 0 || record.projectVO.status=== 3)) && hideBuquBtn()"
              @click="handleUpdate(record)"
            >编辑</a>
            <a
              v-if="record.tageCode == 'PD' && ( record.projectVO.status== 2 || record.projectVO.status== 4)"
              v-btnPermission="'pm_044_imp_03'"
              @click="handleImplementationManagement(record)"
            >实施管理</a>
            <a
              v-if="record.tageCode == 'PD' && record.projectVO.status !== 0"
              @click="$bus.$emit('showApprovalDetailsDialog', record.projectVO.scheduleCompletionPlanId)"
            >审批信息</a>
            <a
              v-if="(record.tageCode == 'PD' && (record.projectVO.status === 0 || record.projectVO.status === 3)) && hideBuquBtn()"
              style="color:red"
              @click="handleDelete(record)"
            >
              删除
            </a>
          </MoreButton>
        </template>
      </s-table>
    </a-card>
    <!-- 新增计划 -->
    <BaseModal
      :visible="visible"
      title="新增竣工收尾工作计划"
      :width="900"
      destroy-on-close
      @cancel="handleModalCancel"
    >
      <custom-form
        ref="form"
        :is-validate="true"
        :edit-model="''"
        :rules="rules"
        :form="form"
        :form-list="formOption"
        @selectOrg="selectOrg"
      />
      <template slot="footer">
        <a-button
          :loading="loading"
          @click="handleModalCancel"
        >
          取消
        </a-button>
        <a-button
          type="primary"
          :loading="loading"
          @click="handleAdd"
        >
          确定
        </a-button>
      </template>
    </BaseModal>
    <!-- 新增计划 -->
  </base-breadcrumb>
</template>
<script>
import { searchOption, columns, statusMap } from './model.js'
import { list } from '@/api/construction/constructionCompleted'
import BaseModal from '@/components/BaseModal'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import MoreButton from '@/components/MoreButton'
export default {
  name: 'constructionCompleted' + Date.now(),
  components: {
    BaseModal,
    MoreButton
  },
  filters: {
    statusFilter(type) {
      return statusMap[type] && statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type] && statusMap[type].status
    }
  },
  data() {
    return {
      // 查询参数
      searchData: {},
      // 搜索选项
      searchOption,
      // 表头
      columns,
      // 统计
      total: 0,
      // 默认展开项
      expandedRowKeys: [],
      // 新增模态框显示隐藏
      visible: false,
      // 新增表单
      form: {
        // projectId: ''
      },
      // 新增中
      loading: false
    }
  },
  computed: {
    // 新增表单项
    formOption() {
      return [
        {
          label: '所属单位',
          type: 'orgTree',
          keyName: 'projectId',
          checkStrictly: true,
          isAutoSelectFirst: true,
          rules: [{ required: true, message: '请选择所属单位', trigger: 'change' }],
          span: 24,
          isNeedAuth: true
        }
        // {
        //   label: '分公司',
        //   keyName: 'branchComId',
        //   type: SelectSubCompany,
        //   rules: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        //   attrs: {
        //     // autoSelected: true
        //   },
        //   span: 12
        // },
        // {
        //   label: '项目名称',
        //   type: SelectProject,
        //   keyName: 'projectId',
        //   rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
        //   attrs: {
        //     // autoSelected: true,
        //     keyValue: 'branchComId',
        //     isEdit: true,
        //     form: this.form
        //   },
        //   span: 12
        // }
      ]
    },
    // 表单校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  created() {
    this.getWbsTemplateList()
  },
  methods: {
    selectOrg(val) {
      if (val && val.length) {
        this.form.branchComId = val[val.length - 2].orgId
        this.form.projectId = val[val.length - 1].projectId
        console.log(val, this.form.projectId, ' this.form.projectId')
        this.$forceUpdate()
      }
      this.$refs.form.clearValidate('projectId')
    },
    /**
     * @description: 获取项目业态
     * @param {void}
     * @return {void}
     */
    getWbsTemplateList() {
      list.wbstemplate({ pageNo: 1, pageSize: 2000 }).then((res) => {
        this.searchOption[1].data = res.data.map((item) => {
          return {
            label: item.formatName,
            value: item.formatId
          }
        })
      })
    },
    /**
     * @description: 表格数据加载方法
     * @param {Object} parameter 分页信息
     * @return {Promise} 接口Promise
     */
    async loadData(parameter) {
      try {
        const {
          data: { treeVO, total, expandedRowKeys }
        } = await list.queryScheduleCompletionPlanTree(this.searchData)
        this.total = total
        this.expandedRowKeys = expandedRowKeys
        return Promise.resolve({
          data: treeVO ? [treeVO] : []
        })
      } catch (error) {
        console.log(error)
        return Promise.reject(error)
      }
    },
    /**
     * @description: 搜索
     * @param {Object} value 搜索数据
     * @return {Void}
     */
    search(value) {
      this.searchData = value || {}
      this.$refs.table.refresh(true)
    },
    /**
     * @description: 查看
     * @param {object} record 行数据
     * @return {void}
     */
    handleDetail({ projectVO: { projectId, scheduleCompletionPlanId, status } }) {
      this.$router.push({
        path: '/construction/constructionCompleted/constructionCompletedView',
        query: {
          projectId,
          scheduleCompletionPlanId,
          pageType: 'detail',
          status
        }
      })
    },
    /**
     * @description: 编辑
     * @param {object} record 行数据
     * @return {void}
     */
    handleUpdate({ projectVO: { projectId, status, scheduleCompletionPlanId } }) {
      this.$router.push({
        path: '/construction/constructionCompleted/constructionCompletedView',
        query: {
          projectId,
          scheduleCompletionPlanId,
          pageType: 'update',
          status
        }
      })
    },
    /**
     * @description: 实施管理
     * @param {object} record 行数据
     * @return {void}
     */
    handleImplementationManagement({ projectVO: { projectId, status, scheduleMasterPlanId: planId, scheduleCompletionPlanId } }) {
      this.$router.push({
        path: '/construction/constructionCompleted/completedImplementationManagement',
        query: {
          projectId,
          scheduleCompletionPlanId,
          status,
          planId
        }
      })
    },
    /**
     * @description: 手动展开
     * @param {boolean} expanded 是否展开
     * @param {object} record 行
     * @return {void}
     */
    expandedRowsChange(expanded, record) {
      if (expanded) {
        this.expandedRowKeys.push(record.orgId)
      } else {
        this.expandedRowKeys.splice(
          this.expandedRowKeys.findIndex((el) => el === record.orgId),
          1
        )
      }
    },
    /**
     * @description: 取消新增
     * @param {void}
     * @return {void}
     */
    handleModalCancel() {
      this.visible = false
      this.form = {
        // projectId: ""
      }
    },
    /**
     * @description: 新增计划
     * @param {void}
     * @return {void}
     */
    handleAdd() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          list
            .createCompletionPlan(this.form)
            .then(({ code }) => {
              if (code === 200) {
                this.handleModalCancel()
                this.$refs.table.refresh(true)
                this.$message.success('新增成功')
              }
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    /**
     * @description: 删除
     * @param {object} record 行
     * @return {void}
     */
    handleDelete(record) {
      const {
        projectVO: { scheduleCompletionPlanId: id }
      } = record
      const vm = this
      this.$confirm({
        title: '温馨提示',
        content: '确认删除该计划？',
        type: 'warning',
        onOk() {
          list.deleteCompletionPlan({ id }).then(({ code }) => {
            if (code === 200) {
              vm.$message.success('操作成功')
              vm.$refs.table.refresh(true)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
